﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using personalplaner.common.mvvm;
using System.Windows;
using personalplaner.gui.common;
using System.Windows.Input;
using personalplaner.common.mvvm.commands;
using personalplaner.business.stamm;
using personalplaner.servicemodel.dataaccess;
using System.Collections.ObjectModel;
using personalplaner.gui.Properties;
using personalplaner.business;

namespace personalplaner.gui.stamm.viewmodels
{
	public class EditSaldoViewModel : BaseViewModel
	{
		public EditSaldoViewModel(DependencyObject parent)
            : base(parent)
        {
        }

		public override void Loaded()
		{
			var mw = GetService<IMainWindowDisplayContainer>();
			mw.IsWaitVisible = false;
		}

		#region Commands

		ICommand _okcommand;
		public ICommand OkCommand
		{
			get
			{
				if (_okcommand == null)
					_okcommand = new RelayCommand(OnOkCommand);
				return _okcommand;
			}
		}

		private void OnOkCommand(object parameter)
		{
			var wnd = Parent as Window;
			if (wnd != null)
			{
				wnd.DialogResult = true;
				wnd.Close();
				return;
			}

			var mw = GetService<IMainWindowDisplayContainer>();
			mw.MoveToPreviousView();
		}

		ICommand _cancelcommand;
		public ICommand CancelCommand
		{
			get
			{
				if (_cancelcommand == null)
					_cancelcommand = new RelayCommand(OnCancelCommand);
				return _cancelcommand;
			}
		}

		private void OnCancelCommand(object parameter)
		{
			var wnd = Parent as Window;
			if (wnd != null)
			{
				wnd.DialogResult = false;
				wnd.Close();
				return;
			}

			var mw = GetService<IMainWindowDisplayContainer>();
			mw.MoveToPreviousView();
		}

		ICommand _savecommand;
		public ICommand SaveCommand
		{
			get
			{
				if (_savecommand == null)
					_savecommand = new RelayCommand(OnSave, parameter => SelectedSaldoAnpassug != null && !string.IsNullOrEmpty(SelectedSaldoAnpassug.Reason) && SelectedSaldoAnpassug.IsNew);
				return _savecommand;
			}
		}

		private void OnSave(object parameter)
		{
			if (SelectedSaldoAnpassug == null)
				return;

            if (Plan != null && (SelectedSaldoAnpassug.Datum < Plan.Von || SelectedSaldoAnpassug.Datum > Plan.Bis))
            {
                MessageBox.Show(string.Format(Resources.DateInsidePlandateFormat,Plan.Von.ToShortDateString(),Plan.Bis.ToShortDateString()),Resources.SaldoEdit, MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

			var da = GetService<IPlanDataAccess>();
			var saldoId = da.SetSaldoAnpassung(SelectedSaldoAnpassug);

			// set the id and name so that IsNew = false and add it to the collection
			SelectedSaldoAnpassug.ID = saldoId;
            SelectedSaldoAnpassug.SaldoId = saldoId;
			SelectedSaldoAnpassug.Name = "SaldoAnpassung";
			SaldoAnpassungen.Add(SelectedSaldoAnpassug);

			Mediator.NotifyColleagues<SaldoAnpassung>("RefreshPersonSaldo", SelectedSaldoAnpassug);

			OnNew(null);
		}

		ICommand _newcommand;
		public ICommand NewCommand
		{
			get
			{
				if (_newcommand == null)
					_newcommand = new RelayCommand(OnNew, p => SelectedPlanLayoutItem != null);
				return _newcommand;
			}
		}

		private void OnNew(object parameter)
		{
			if (SelectedPlanLayoutItem == null)
				return;
            
			SelectedSaldoAnpassug = new SaldoAnpassung()
			{
				Datum = Plan != null ? Plan.Von : DateTime.Now,
				PlanId = PlanId,
				AbteilungId = SelectedPlanLayoutItem.AbteilungID,
				PrsId=SelectedPlanLayoutItem.PrsID
			};

			RaiseMoveFocus("Amount");
		}

		ICommand _deleteCommand;
		public ICommand DeleteCommand
		{
			get
			{
				if (_deleteCommand == null)
					_deleteCommand = new RelayCommand(OnDelete, param => SelectedSaldoAnpassug != null && !SelectedSaldoAnpassug.IsNew);
				return _deleteCommand;
			}
		}

		private void OnDelete(object parameter)
		{
			if (MessageBox.Show(Resources.SindSicherEintragLoeschen, Resources.EintragLoeschen, MessageBoxButton.YesNo, MessageBoxImage.Exclamation) != MessageBoxResult.Yes)
				return;
                
			var da = GetService<IPlanDataAccess>();
			da.DeleteSaldoAnpassung(SelectedSaldoAnpassug);

			SaldoAnpassungen.Remove(SelectedSaldoAnpassug);

			SelectedSaldoAnpassug = null;

			Mediator.NotifyColleagues<SaldoAnpassung>("RefreshPersonSaldo", SelectedSaldoAnpassug);
		}

		#endregion

		#region Properties

		int _schemaid;
		public int SchemaId
		{
			get
			{
				return _schemaid;
			}
			set
			{
				_schemaid = value;
				RaisePropertyChanged("SchemaId");

				var da = GetService<IPlanSchemaDataAccess>();
				var lst = da.GetPersonenInSchema(value);

				if (lst == null)
				{
					PlanLayoutItems = new ObservableCollection<PlanSchemaItem>();
					return;
				}

				if (PlanLayoutItems == null)
					PlanLayoutItems = new ObservableCollection<PlanSchemaItem>(lst);
				else
				{
					PlanLayoutItems.Clear();
					foreach (var itm in lst)
						PlanLayoutItems.Add(itm);
				}

                if (PlanId == 0)
                {
                    var pda = GetService<IPlanDataAccess>();
                    PlanId = pda.GetPlanId(SchemaId);
                }
			}
		}

		public int PlanId { get; set; }

        ArbeitsPlanHeader _plan;
        public ArbeitsPlanHeader Plan
        {
            get
            {
                if (_plan == null)
                {
                    var da = GetService<IPlanDataAccess>();
                    _plan = da.GetArbeitsPlan(PlanId);
                }
                return _plan;
            }
        }

		IList<PlanSchemaItem> _planLayoutItems;
		public IList<PlanSchemaItem> PlanLayoutItems
		{
			get
			{
				return _planLayoutItems;
			}
			set
			{
				_planLayoutItems = value;
				RaisePropertyChanged("PlanLayoutItems");
			}
		}

		PlanSchemaItem _selectedPlanLayoutItem;
		public PlanSchemaItem SelectedPlanLayoutItem
		{
			get { return _selectedPlanLayoutItem; }
			set
			{
				_selectedPlanLayoutItem = value;
				RaisePropertyChanged("SelectedPlanLayoutItem");

				var da = GetService<IPlanDataAccess>();
				var lst = da.GetSaldoAnpassungen(value.PrsID, value.AbteilungID, PlanId);

				if (SaldoAnpassungen == null)
					SaldoAnpassungen = lst != null ? new ObservableCollection<SaldoAnpassung>(lst) : new ObservableCollection<SaldoAnpassung>();
				else
				{
					SaldoAnpassungen.Clear();

					if (lst == null)
						return;

					foreach (var itm in lst)
						SaldoAnpassungen.Add(itm);
				}
			}
		}

		IList<SaldoAnpassung> _saldoAnpassungen;
		public IList<SaldoAnpassung> SaldoAnpassungen
		{
			get
			{
				return _saldoAnpassungen;
			}
			set
			{
				_saldoAnpassungen = value;
				RaisePropertyChanged("SaldoAnpassungen");
			}
		}

		SaldoAnpassung _selectedSaldoAnpassung;
		public SaldoAnpassung SelectedSaldoAnpassug
		{
			get
			{
				return _selectedSaldoAnpassung;
			}
			set
			{
				_selectedSaldoAnpassung = value;
				RaisePropertyChanged("SelectedSaldoAnpassug");
				RaisePropertyChanged("IsWriteable");
			}
		}

		public bool IsWriteable
		{
			get
			{
				return SelectedSaldoAnpassug != null && SelectedSaldoAnpassug.IsNew;
			}
		}

		#endregion
	}
}
